package case11

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func Test() {
	fmt.Println("🎯示例十一：获取form表单参数")

	r := gin.Default()

	r.POST("/addUser", func(c *gin.Context) {

		// 获取name参数
		//var name string = c.PostForm("name")

		// 当name没有传入，使用默认赋值获取
		var name string = c.DefaultPostForm("name", "LILI")

		// 获取age参数
		var ageStr string = c.PostForm("age")
		age, err := strconv.Atoi(ageStr)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{
				"code":    http.StatusInternalServerError,
				"message": "age参数有误!",
			})
		}

		// 获取爱好数组参数
		var hobby []string = c.PostFormArray("hobby")

		c.JSON(http.StatusOK, gin.H{
			"name":  name,
			"age":   age,
			"hobby": hobby,
		})
	})

	r.Run(":60000")
}
